﻿#include "mainwindow.h"
#include <QApplication>
#include "loginwindow.h"
#include "dbtools.h"
#include <QSettings>
#include <QDebug>
#include "firststartdialog.h"
#include <stdio.h>
#include <stdlib.h>
#include "utils.h"
#include <QSqlQuery>
#include <QProcess>

void myMessageOutPut(QtMsgType type,const QMessageLogContext &context,const QString &msg){
    QByteArray localMsg = msg.toLocal8Bit();
        switch (type) {
        case QtDebugMsg:
            fprintf(stderr, "Debug: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
            break;
        case QtInfoMsg:
            fprintf(stderr, "Info: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
            break;
        case QtWarningMsg:
            fprintf(stderr, "Warning: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
            break;
        case QtCriticalMsg:
            fprintf(stderr, "Critical: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
            break;
        case QtFatalMsg:
            fprintf(stderr, "Fatal: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
            break;
        }
}

int main(int argc, char *argv[])
{
    //qInstallMessageHandler(myMessageOutPut);
    QApplication a(argc, argv);
    //设置格式化输出
    //qSetMessagePattern("[%{time yyyyMMdd h:mm:ss.zzz t} %{if-debug}D%{endif}%{if-info}I%{endif}%{if-warning}W%{endif}%{if-critical}C%{endif}%{if-fatal}F%{endif}] %{file}:%{line} - %{message}");

    //设置应用信息
    QCoreApplication::setOrganizationName("atnoce");
    QCoreApplication::setOrganizationDomain("atnoce.com");
    QCoreApplication::setApplicationName("OnePassX");
    QCoreApplication::setApplicationVersion("1.0.0.6");

    QSettings sett;
    Dbtools *db = new Dbtools;

    qDebug()<<QCoreApplication::applicationDirPath();

    //判断是否第一次启动
    //QSettings setting;

    if(sett.contains("app/firstStart") && sett.value("app/firstStart").toString()=="N"){

        qDebug()<<"不是第一次启动";
        //启动登录页面

        LoginWindow *login = new LoginWindow(db);
        login->show();
    }else{
        //是第一次启动
        qDebug()<<"是第一次启动";
        //启动引导界面
        FirstStartDialog *firstDlg = new FirstStartDialog;
        firstDlg->show();
    }
    qDebug()<<"应用程序PID："<<QCoreApplication::applicationPid()<<",版本:"
           <<QCoreApplication::applicationVersion()<<",名称："<<
             QCoreApplication::applicationName()<<",组织："<<
             QCoreApplication::organizationName()<<",域名："<<
             QCoreApplication::organizationDomain();



    int e = a.exec();
    if(e==773){
        QProcess::startDetached(qApp->applicationFilePath(),QStringList());
        return 0;
    }
    return e;
}


